<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="180" header-align="center"></el-table-column>
      <el-table-column prop="pinyin" label="拼音" width="180" header-align="center"></el-table-column>
      <el-table-column prop="logo" label="LOGO" width="180" header-align="center"></el-table-column>
      <el-table-column prop="description" label="简介" width="180" header-align="center"></el-table-column>
      <el-table-column prop="keywords" label="关键词列表" header-align="center"></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
      <el-table-column prop="sales" label="销量" width="100" align="center"></el-table-column>
      <el-table-column label="是否启用" width="120" align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="changeEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#cccccc">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     :disabled="scope.row.isParent == 1"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出的编辑品牌的对话框 -->
    <el-dialog title="修改品牌" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="拼音" :label-width="formLabelWidth">
          <el-input v-model="editForm.pinyin" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="LOGO" :label-width="formLabelWidth">
          <el-input v-model="editForm.logo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" :label-width="formLabelWidth">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="关键词列表" :label-width="formLabelWidth">
          <el-input v-model="editForm.keywords" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" :label-width="formLabelWidth">
          <el-input v-model="editForm.sort" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="销量" :label-width="formLabelWidth">
          <el-input v-model="editForm.sales" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" :label-width="formLabelWidth">
          <el-input v-model="editForm.enable" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentParentId: 0,
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        id:'',
        name: '',
        pinyin: '',
        logo: '',
        description: '',
        keywords: '',
        sort: '',
        sales: '',
        enable :''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {

    // changeEnable(category) {
    //   let enableText = ['禁用', '启用'];
    //   let url = 'http://localhost:9080/categories/' + category.id + '/';
    //   if (category.enable == 1) {
    //     url += 'enable';
    //   } else {
    //     url += 'disable';
    //   }
    //   console.log('url = ' + url);
    //   this.axios.post(url).then((response) => {
    //     let responseBody = response.data;
    //     if (responseBody.state == 20000) {
    //       let message = '操作成功，已经将【' + category.name
    //           + '】的状态改为【' + enableText[category.enable] + '】！';
    //       this.$message({
    //         message: message,
    //         type: 'success'
    //       });
    //     } else {
    //       this.$message.error(responseBody.stateMessage);
    //       this.loadCategoryList();
    //     }
    //   });
    // },


    //编辑按钮，修改信息方法
    openEditDialog(brand) {
      this.dialogFormVisible = true;
    },
    submitEditForm() {
      let url = 'http://localhost:9080/brands/' + this.editForm.id + '/update'
      let formData = this.qs.stringify(this.editForm);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = false;
          this.$message.success('修改品牌信息成功！');
          this.loadCategoryList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.stateMessage, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadCategoryList();
            }
          });
        } else {
          this.$message.error(responseBody.stateMessage);
        }
      });
    },

    //删除按钮，删除方法
    openDeleteConfirm(brand) {
      let message = '此操作将永久删除【' + brand.name + '】品牌，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(brand);
      }).catch(() => {
      });
    },
    handleDelete(brand) {
      let url = 'http://localhost:9080/brands/' + brand.id + "/delete";
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message.success('删除成功！');
          this.loadCategoryList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.stateMessage, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        } else {
          this.$message.error(responseBody.stateMessage);
        }
      });
    },

    //页面展示，查询数据列表
    loadCategoryList() {
      let url = 'http://localhost:9080/brands/list';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.tableData = responseBody.data;
        }
      });
    }
  },
  mounted() {
    this.loadCategoryList();
  }
}
</script>